using System.Collections;
using System.Collections.Generic;
using UnityEditor;
using UnityEngine;
using UnityEngine.UIElements.Experimental;

public class PlayerComponent : MonoBehaviour {
    public float playerSize;
    public float moveForce;

    Vector2 moveDir;
    Rigidbody2D rb;


    void Start() {
        rb = GetComponent<Rigidbody2D>();
        transform.localScale = Vector3.one * playerSize;
    }

    void Update() {
        //上下左右移动
        float h = Input.GetAxis("Horizontal");
        float v = Input.GetAxis("Vertical");
        moveDir = new Vector2(h, v).normalized;

        // 左Shift一键变大
        if (Input.GetKeyDown(KeyCode.LeftShift)) {
            Grow(1);
        }
    }

    private void FixedUpdate() {
        rb.AddForce(moveDir * moveForce);
    }

    private void OnTriggerEnter2D(Collider2D collision) {
        if (collision.gameObject.GetComponent<OtherCircle>() != null) {
            if (playerSize > collision.GetComponent<OtherCircle>().circleSize) {
                Destroy(collision.gameObject);
                Grow(collision.GetComponent<OtherCircle>().circleSize);
            }
            else {
                Die();
            }
        }
    }

    void Grow(float upValue) {
        playerSize += upValue / 5f;
        transform.localScale = Vector3.one * playerSize;

        GameManager.Instance.GainScore((int)(upValue * 100f));
    }

    void Die() {
        GameManager.Instance.GameOver();
    }


}
